package com.bruce.dao.impl;

import com.bruce.bean.Major;
import com.bruce.dao.MajorDao;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;

public class MajorDaoImpl extends BaseDao implements MajorDao {

    @Override
    public List<Major> findMajorsByCollegeId(int collegeId) {
        try {
            return queryRunner.query("select * from major where collegeid=?",
                    new BeanListHandler<Major>(Major.class),collegeId);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public String findMajorName(int parseInt) {
        try {
            String query = queryRunner.query("select title from college where id=?", new ScalarHandler<>(), parseInt);
            return query;
        } catch (SQLException sqlException) {
            sqlException.printStackTrace();
        }
        return null;
    }

    @Override
    public int addcourse(int grade, String cid, String sid) {
        StringBuffer sql = new StringBuffer("update grade set grade=? where cid=? and sid=?");
        int update = 0;
        try {
             update = queryRunner.update(sql.toString(), grade, cid, sid);
        } catch (SQLException sqlException) {
            sqlException.printStackTrace();
        }
        return update;
    }
}
